package com.jingdianjichi.wx.handler;


import lombok.SneakyThrows;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 工厂模式，建立枚举到策略模式的映射，实现自适应的Bean对象返回/创建
 * @return:
 * @Author:  sky
 * @date:  2024/11/13 22:51
 */
@Component
public class WxChatMsgFactory implements InitializingBean {

    @Autowired
    private List<WxChatMsgHandler> wxChatMsgHandlerList;

    private Map<WxChatMsgTypeEnum,WxChatMsgHandler> handlerMap = new HashMap<>();


    @Override
    @SneakyThrows
    public void afterPropertiesSet(){
        for (WxChatMsgHandler wxChatMsgHandler:wxChatMsgHandlerList){
            handlerMap.put(wxChatMsgHandler.getMsgType(),wxChatMsgHandler);
        }
    }

    public WxChatMsgHandler getHandlerByMsgType(String msgType){
        return handlerMap.get(WxChatMsgTypeEnum.getByMsgType(msgType));
    }

}
